package jp.sourceforge.akjrcp.subtitle;

import java.io.File;

import jp.sourceforge.akjrcp.akj_betas.Messages;
import jp.sourceforge.akjrcp.commons.ui.EditableFileList;
import jp.sourceforge.akjrcp.commons.ui.FileDirChoicer;
import jp.sourceforge.akjrcp.commons.ui.FileDirChoicer2;
import jp.sourceforge.akjrcp.commons.ui.AbstractFileDirChoicer.FileStatusChangeEvent;
import jp.sourceforge.akjrcp.commons.ui.AbstractFileDirChoicer.FileStatusChangeListener;
import jp.sourceforge.akjrcp.commons.ui.widget.WidgetBuilder;
import jp.sourceforge.akjrcp.commons.util.FileNameUtils;
import jp.sourceforge.akjrcp.ui.LayoutDataBuilder;
import jp.sourceforge.akjrcp.ui.param.ParamScale;
import jp.sourceforge.akjrcp.util.ExecutingControler;
import jp.sourceforge.akjrcp.util.ExecutingControler.ExecutingControlButton;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;

public class SrtUtilView extends ViewPart{
	public static Log log = LogFactory.getLog(SrtUtilView.class);

	public static final String ID = SrtUtilView.class.getName();

	SrtUtilExecute executer;

	private EditableFileList fileList;

	private ParamScale strechTimeSale;

	private FileDirChoicer2 outputDirChooser;
	@Override
	public void createPartControl(Composite parent) {
		executer=new SrtUtilExecute();
		parent.setLayout(new GridLayout());
		
		
		strechTimeSale = new ParamScale(parent,SWT.NULL,"strech_time",Messages.getString("wav2midi.strech_time")){
			protected String toValue(String n){
				double num=Integer.parseInt(n);
				String ret=""+(double)getScale().getSelection()/10;
				while(ret.length()<num){
					ret=" "+ret;	
					}
				return ret;
			}
		};
strechTimeSale.setMinimum(3);
strechTimeSale.setMaximum(30);
strechTimeSale.setValue(10);
		
		fileList = WidgetBuilder.createFileList(parent,SWT.BORDER,"*.srt");
		fileList.setLayoutData(LayoutDataBuilder.fillBothGridData());
		fileList.setInput(this.getSite());
		fileList.addPropertyChangeListener(new IPropertyChangeListener(){

			public void propertyChange(PropertyChangeEvent event) {
				executer.updateControlSwitch();
			}});
		
		outputDirChooser = new FileDirChoicer2(parent,SWT.BORDER,FileDirChoicer.DIR,Messages.getString("srtutil.save_dir"),false);
		outputDirChooser.setLayoutData(LayoutDataBuilder.fillHorizontalGridData());
		outputDirChooser.addSelectableFileName(FileNameUtils.LOCAL_NAME_MY_DOCUMENTS);
		outputDirChooser.addSelectableFileName(FileNameUtils.LOCAL_NAME_DESKTOP);
		outputDirChooser.addSelectableFileName(FileNameUtils.LOCAL_NAME_MY_PICTURE);
		outputDirChooser.addFileListChangeListener(new FileStatusChangeListener(){

			public void addList(FileStatusChangeEvent event) {
				executer.updateControlSwitch();
			}

			public void removeList(FileStatusChangeEvent event) {
				executer.updateControlSwitch();
				
			}});
		//exec buttons
		ExecutingControlButton exec=new ExecutingControlButton(parent,SWT.NULL,executer,
				Messages.getString("exec"),Messages.getString("cancel"));
		exec.setLayoutData(LayoutDataBuilder.hintGridData(100,40));
		executer.updateControlSwitch();
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub
		
	}

	public class SrtUtilExecute extends ExecutingControler{

		private double strechPlayTime;
		private File srtFiles[];
		@Override
		public void afterExecute() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void beforeExecute() {
			strechPlayTime=Double.parseDouble(strechTimeSale.getValue())/10;
			
		}

		@Override
		public boolean canExecute() {
			return fileList.countFiles()>0 && !outputDirChooser.getAbsoluteFilePath().equals("");
			
		}

		@Override
		public void cancelExecuting() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void inExecute() {
			// TODO Auto-generated method stub
			
		}
		
	}
}
